#include "StdAfx.h"
#include "Cylinder.h"
#include "GL/glu.h"
#define M_PI       3.14159265358979323846
CCylinder::CCylinder(void)
{
	rotateY = 75;
}

CCylinder::CCylinder(CPoint3D pBegin, CPoint3D pEnd)
{
	m_Begin = pBegin;
	m_End = pEnd;
}

void CCylinder::Draw()
{
	float x = (m_Begin.getX() + m_End.getX()) / 2;
	float y = (m_Begin.getY() + m_End.getY()) / 2;
	float z = (m_Begin.getZ() + m_End.getZ()) / 2;
	float h = abs(m_Begin.getX() - m_End.getX());
	float r = abs(m_Begin.getY() - m_End.getY()) / 2;
	if (h == 0) return;
	
	glPushMatrix();
	glTranslatef(x, y, z);

	glRotatef(rotateX, 1.0, 0.0, 0.0);
	glRotatef(rotateY, 0.0, 1.0, 0.0);
	glRotatef(rotateZ, 0.0, 0.0, 1.0);

	if (selected == true)
	{
		glMaterialfv(GL_FRONT, GL_AMBIENT, blue);
	}
	else 
	{
		//glMaterialfv(GL_FRONT, GL_DIFFUSE, black);
		glMaterialfv(GL_FRONT, GL_AMBIENT, black);
	}
	
	GLUquadricObj *quadratic = gluNewQuadric();
	gluQuadricNormals(quadratic, GLU_FLAT);
	gluQuadricDrawStyle(quadratic, GLU_FILL); /* flat shaded */
	//gluCylinder(quadratic, 0.5, 0.5, 1.0, 15, 5);

	gluCylinder(quadratic,r,r,h,32,32);


	//for(float slice = -h; slice <= h; slice += 2*h)
	//{
	//	glBegin(GL_POLYGON);
	//		for(float angle = 0.0 ; angle < 360.0 ; angle +=  10 ) 
	//		{
	//			glVertex3f(r*cos(angle*DEG_2_RAD), slice, r*sin(angle*DEG_2_RAD));				
	//		}
	//	glEnd();
	//}
	////Draw body
	////glColor3f(0.0, 1.0, 0.0);
	//glBegin(GL_TRIANGLE_STRIP);
	//for(float angle = 0.0 ; angle <= 360.0 ; angle +=  10 ) 
	//{
	//	glVertex3f(r*cos(angle*DEG_2_RAD), -h, r*sin(angle*DEG_2_RAD));
	//	glVertex3f(r*cos(angle*DEG_2_RAD), h, r*sin(angle*DEG_2_RAD));
	//}
	//glEnd();

	glPopMatrix();	
	
}

CCylinder::~CCylinder(void)
{
}
